|
|
|
| String and Numeric with the Plus Sign 1. Double-click the & String/Num command button. In this example, you have to change the plus sign to the ampersand character to concatenate the two values together: | | Private Sub cmdStrNum_Click ()
Dim vntStr1 As Variant
Dim vntStr2 As Variant
vntStr1 = "10"
vntStr2 = 20
lblStrNum.Caption = vntStr1 & vntStr2
End Sub |  | The ampersand tells Visual Basic to use string concatenation if both of the values can be converted to strings. Because you can always convert a numeric value to a string, Visual Basic will be able to perform the concatenation. Therefore, you end up with a concatenated string of 1020 when this example is run. |
|
|
|
|
|
|
When performing string concatenation on two variant values, you must take extra care to use the correct operator. You need to know what the data type is for both the operators and choose the appropriate operator. |
|
|
|
|
|
|
|
|
The standard for most Visual Basic programmers is to always use the ampersand (&) operator when concatenating two strings together. This leaves little ambiguity as to what operation you are actually performing. Of course, this strange behavior between the plus sign and the ampersand only applies to Variant variables, so you should not typically have this problem. |
|
|
|
|
|
|
|
|
Checking the Type of a Variant |
|
|
|
|
|
|
|
|
To help you determine which operator to use on two different variants, the VarType() function is built into the Visual Basic language to enable you to test the data type of the variables in the variants. This function returns a number that corresponds to a constant that you can use to determine the data type. Table 6.6 lists the data types and their corresponding constants. |
|
|
|
|
|